/**
 * 	
 * 
 */
package de.farbstreich.tozen2.common;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 * This class contains the information which will be send/received by client and
 * server. It decodes and encodes the information into/from JSON format(
 * http://www.json.org )
 * 
 * @author Tim Babies, Martin Spielmann
 * 
 */
public class TozenProtocolMessage {
	/**
	 * Type of the Message
	 */
	public TozenMessageType type;

	public Boolean status = true;

	/**
	 * The informations about a game. Is set for: games, game
	 */
	public game[] games;

	public class position {
		public int x;
		public int y;
		public int playerId;
		public int tileId;

		/**
		 * @param x
		 * @param y
		 * @param playerId
		 * @param tileId
		 */
		public position(int x, int y, int playerId, int tileId) {
			this.x = x;
			this.y = y;
			this.playerId = playerId;
			this.tileId = tileId;
		}

	}

	public class game {
		public int id;
		public String title;
		public String name;
		public int maxPlayersCount;
		public int minPlayersCount;
		public int playersCount;
		public ArrayList<position> positions;
		public ArrayList<player> players;
		public HashMap<String, Object> options;

		public game() {
			this.positions = new ArrayList<position>();
			this.players = new ArrayList<player>();
		}

	}

	/**
	 * Informations about a player. Set for: players, sendMessage, setName,
	 * winGame, currentPlayer, message
	 */
	public ArrayList<player> players;

	public class player {
		public int id;
		public String name;
		public int color;
		public boolean ingame;

		/**
		 * @param id
		 * @param name
		 * @param color
		 */
		public player(int id, String name, int color) {
			this.id = id;
			this.name = name;
			this.color = color;
			this.ingame = false;
		}

		public player() {
			this.ingame = false;
		}

		public player(int id) {
			this.id = id;
			this.ingame = false;
		}
		
		public player(String name) {
			this.name = name;
		}

	}

	public gameType[] gameTypes;

	public class gameType {
		public int id;
		public String title;
		public int maxPlayersCount;
		public int minPlayersCount;
		public HashMap<String, Object> options;
	}

	public int startX;
	public int startY;
	public int targetX;
	public int targetY;
	public boolean validity;

	public String message;
	public boolean broadcast;

	/**
	 * To decode a Message, use this constructor
	 * 
	 * @param msg
	 *            
	 */
	public TozenProtocolMessage(String msg) throws ParseException {
		init();
		Object obje = JSONValue.parse(msg);
		JSONObject obj = (JSONObject) obje;

		status = (Boolean) obj.get("status");

		TozenProtocolDecoder decoder = new TozenProtocolDecoder(this, obj);
		decoder.decode();

	}

	public TozenProtocolMessage(TozenMessageType type) {
		init();
		this.type = type;
		this.status = true; // Default value

	}

	/**
	 * General Constructor to initalize Objects..
	 */
	private void init() {
		players = new ArrayList<player>();

	}

	/**
	 * This constructor encodes information filled into this class into the
	 * JSON-format and creates a String out of it.
	 * 
	 * @return
	 */
	public String toString() throws ParseException {
		JSONObject obj = new JSONObject();

		TozenProtocolEncoder encoder = new TozenProtocolEncoder(this, obj);
		obj = encoder.encode();

		return obj.toJSONString();

	}

	private String getValue(String line) {
		return (String) line.subSequence(line.indexOf(" "), line.length());
	}
}
